<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card style="margin-top:20px;">
      <el-row>
        <el-col>
          <el-button type="success" @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <template slot="isok" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level===0">一级权限</el-tag>
          <el-tag v-else-if="scope.row.cat_level===1" type="warning">二级权限</el-tag>
          <el-tag v-else type="danger">三级权限</el-tag>
        </template>
        <template slot="quanxian" slot-scope="scope">
          <i class="el-icon-success" style="color:green;" v-if="scope.row.cat_deleted===false"></i>
          <i class="el-icon-error" style="color:red;" v-else></i>
        </template>
        <template slot="caozuo" slot-scope="scope">
          <el-button class="el-icon-delete" @click="delCate(scope.row)" type="danger">删除</el-button>
          <el-button class="el-icon-edit" @click="ediCate(scope.row)" type="warning">修改</el-button>
        </template>
      </tree>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 增加类别对话框 -->
    <el-dialog title="增加分类" :visible.sync="addDialogVisible" width="50%" @close="addClose">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <!-- 级联选择器 -->
        <el-form-item label="父级分类">
          <el-cascader
            clearable
            change-on-select
            v-model="cateValue"
            :options="cateOptions"
            @change="handleChange"
            :props="cateProps"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMethod">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改类别对话框 -->
    <el-dialog title="修改分类" :visible.sync="ediDialogVisible" width="50%" @close="ediClose">
      <el-form :model="ediForm" :rules="ediFormRules" ref="ediFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ediForm.cat_name"></el-input>
        </el-form-item>
        <!-- 级联选择器 -->
        <!-- <el-form-item label="父级分类">
          <el-cascader
            clearable
            change-on-select
            v-model="cateValue"
            :options="cateOptions"
            @change="handleChange"
            :props="cateProps"
          ></el-cascader>
        </el-form-item>-->
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="ediDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="ediMethod">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 分类源数据
      cateList: [],
      // 配置对象
      columns: [
        {
          label: "分类名称",
          width: "150",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          width: "150",
          type: "template",
          template: "isok"
        },
        {
          label: "权限等级",
          width: "100",
          type: "template",
          template: "quanxian"
        },
        {
          label: "操作",
          type: "template",
          template: "caozuo"
        }
      ],
      // 分页参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //   总页数
      total: 0,
      //   增加对话框显示条件
      addDialogVisible: false,
      addForm: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0
      },
      //   表单验证
      addFormRules: {
        cat_name: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ]
      },
      //   修改对话框显示条件
      ediDialogVisible: false,
      //   表单验证
      ediFormRules: {
        cat_name: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ]
      },
      ediForm: {
        cat_name: ""
      },
      //   级联选择器绑定model
      cateValue: [],
      //   级联选择器绑定源数据
      cateOptions: [],
      cateProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children"
      }
    };
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      //   console.log(res.data);
      this.total = res.data.total;
      this.cateList = res.data.result;
      //   console.log(res.data);
    },
    // 增加分类事件
    async addCate() {
      // 获取分类数据
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.cateOptions = res.data;
      this.addDialogVisible = true;
    },
    // 增加确认事件
    addMethod() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return;
        const { data: res } = await this.$http.post("categories", this.addForm);
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg);
        this.$message.success(res.meta.msg);
        this.addDialogVisible = false;
        this.getCateList();
      });
    },
    // 增加关闭事件
    addClose() {
      this.$refs.addFormRef.resetFields();
      this.cateValue = [];
      this.addForm.cat_pid = 0;
      this.addForm.cat_level = 0;
    },
    // 删除事件
    async delCate(info) {
      let del = await this.$confirm(
        `此操作将永久删除该${info.cat_name}, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (del !== "confirm") return this.$message.info("取消了删除");
      const { data: res } = await this.$http.delete(
        "categories/" + info.cat_id
      );
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.getCateList();
    },
    // 修改事件
    async ediCate(row) {
      // 获取分类数据
      //   const { data: res2 } = await this.$http.get("categories", {
      //     params: { type: 2 }
      //   });
      //   if (res2.meta.status !== 200) return this.$message.error(res.meta.msg);
      //   this.cateOptions = res2.data;
      // 发起请求获取数据
      const { data: res } = await this.$http.get("categories/" + row.cat_id);
      console.log(res.data.cat_name);
      this.ediForm = res.data;
      //   //   获取数据渲染到级联选择器
      //   // 1级
      //   if (row.cat_level === 0) {
      //     this.cateValue = [];
      //   }
      //   // 二级
      //   else if (row.cat_level === 1) {
      //     this.cateValue[0] = row.cat_pid;
      //   }
      //   // 三级
      //   else {
      //     // 发起请求获取数据
      //     const { data: res1 } = await this.$http.get("categories/" + row.cat_pid);
      //     this.cateValue[0] = res1.data.cat_pid;
      //     this.cateValue[1] = res1.data.cat_id;
      //   }
      //   console.log(this.cateValue);
      this.ediDialogVisible = true;
    },
    // 修改关闭事件
    ediClose() {
      this.$refs.ediFormRef.resetFields();
      this.cateValue = [];
    },
    // 修改确认事件
    async ediMethod() {
      this.$refs.ediFormRef.validate(async valid => {
        if (!valid) return;
        const { data: res } = await this.$http.put(
          "categories/" + this.ediForm.cat_id,
          this.ediForm
        );
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
        this.$message.success(res.meta.msg);
        this.ediDialogVisible = false;
        this.getCateList();
      });
    },
    // 级联选择器发生改变时触发事件
    handleChange() {
      if (this.cateValue.length > 0) {
        this.addForm.cat_pid = this.cateValue[this.cateValue.length - 1];
        this.addForm.cat_level = this.cateValue.length;
      } else {
        this.addForm.cat_pid = 0;
        this.addForm.cat_level = 0;
      }
      //   console.log(this.addForm);
    },
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getCateList();
    },
    handleCurrentChange(newnum) {
      this.queryInfo.pagenum = newnum;
      this.getCateList();
    }
  },
  created() {
    this.getCateList();
  }
};
</script>
<style lang="less" scoped>
</style>